package com.gxp.controller.order;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gxp.pojo.Game;
import com.gxp.pojo.Order;
import com.gxp.pojo.User;
import com.gxp.service.GameService;
import com.gxp.service.OrderService;
import com.gxp.service.UserService;
import com.gxp.util.common.GXPConstant;

@Controller
public class OrderController {
	
	Logger logger = Logger.getLogger(OrderController.class);

	@Autowired
	private GameService gameService;
	
	@Autowired
	private OrderService orderSerivce;
	
	@Autowired
	private UserService userSerivce;

	@RequestMapping(value = "/admin/order/list", method = RequestMethod.GET)
	public ModelAndView manageBuyerGoods() {
		logger.debug("OrderController manageBuyerGoods is invoked.........");
		List<User> users = userSerivce.getAllBuyers();
		List<Game> games = gameService.getGames();
		ModelAndView mav = new ModelAndView();
		mav.addObject("games", games);
		mav.addObject("users", users);
		mav.setViewName("admin/order/game/orderList");
		return mav;
	}
	
	@RequestMapping(value = "/admin/buyerOrder/list", method = RequestMethod.POST)
	@ResponseBody
	public List<Order> searchBuyerGoods(Order searchOrder) {
		
		Map<String,Object> param = new HashMap<String, Object>();
		if (!"0".equals(searchOrder.getUserId())) {
			param.put(GXPConstant.MAPPER_KEY_USER_ID, searchOrder.getUserId());
		}
		
		if (searchOrder.getGameId() != 0) {
			param.put(GXPConstant.MAPPER_KEY_GAME_ID, searchOrder.getGameId());
		}
		
		if (searchOrder.getServerId() != 0) {
			param.put(GXPConstant.MAPPER_KEY_SERVER_ID, searchOrder.getServerId());
		}
		List<Order> orders = orderSerivce.findBuyerOrders(param);
		logger.debug("order list: " + orders.size());
		return orders;
	}
	
	@RequestMapping(value = "/admin/buyerOrder/manage/{userId}", method = RequestMethod.GET)
	public ModelAndView manageBuyerGoodsByUser(@PathVariable String userId) {
		logger.debug("OrderController manageBuyerGoodsByUser is invoked.........");
		List<Game> games = gameService.getGames();
		Map<String, Object> param = new HashMap<String, Object>();
		param.put(GXPConstant.MAPPER_KEY_USER_ID, userId);
		List<Order> orders = orderSerivce.findBuyerOrders(param);
		List<User> users = userSerivce.getAllBuyers();
		ModelAndView mav = new ModelAndView();
		mav.addObject("games", games);
		mav.addObject("orders", orders);
		mav.addObject("selectedUserId", userId);
		mav.addObject("users", users);
		mav.setViewName("admin/order/maintainBuyerOrders");
		return mav;
	}
	
	@RequestMapping(value="/admin/buyerOrder/view/{orderId}",method=RequestMethod.POST)
	@ResponseBody
	public Order viewOrderInfo(@PathVariable int orderId) {
		
		Map<String,Object> param = new HashMap<String, Object>();
		param.put(GXPConstant.MAPPER_KEY_ORDER_ID, orderId);		
		Order order = orderSerivce.findByOrderId(orderId);
		return order;
	}
	
	
	@RequestMapping(value="/admin/buyerOrder/active/{orderId}",method=RequestMethod.POST)
	@ResponseBody
	public List<Order> activeOrder(@PathVariable int orderId, Order searchOrder) {
		Map<String,Object> param = new HashMap<String, Object>();
		param.put(GXPConstant.MAPPER_KEY_ORDER_ID, orderId);
		param.put(GXPConstant.MAPPER_KEY_NEW_STATUS, 1);
		orderSerivce.updateOrderStatus(param);
		
		return searchBuyerGoods(searchOrder);
	}
	
	@RequestMapping(value="/admin/buyerOrder/inActive/{orderId}",method=RequestMethod.POST)
	@ResponseBody
	public List<Order> inActiveOrder(@PathVariable int orderId, Order searchOrder) {
		Map<String,Object> param = new HashMap<String, Object>();
		param.put(GXPConstant.MAPPER_KEY_ORDER_ID, orderId);
		param.put(GXPConstant.MAPPER_KEY_NEW_STATUS, 0);
		orderSerivce.updateOrderStatus(param);
		
		return searchBuyerGoods(searchOrder);
	}
	
	@RequestMapping(value="/admin/buyerOrder/delete/{orderId}",method=RequestMethod.POST)
	@ResponseBody
	public List<Order> delOrder(@PathVariable int orderId, Order searchOrder) {
		Map<String,Object> param = new HashMap<String, Object>();
		param.put(GXPConstant.MAPPER_KEY_ORDER_ID, orderId);
		param.put(GXPConstant.MAPPER_KEY_NEW_STATUS, -1);
		orderSerivce.updateOrderStatus(param);
		
		return searchBuyerGoods(searchOrder);
	}
	
}
